//package com.utils.spring.shirospringboot.controller;
//
//import java.math.BigDecimal;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.LockedAccountException;
//import org.apache.shiro.authc.UnknownAccountException;
//import org.apache.shiro.subject.Subject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.kyhd.dshp.manage.enums.ResultStatusCode;
//import com.kyhd.dshp.manage.model.Login;
//import com.kyhd.dshp.manage.model.User;
//import com.kyhd.dshp.manage.service.MenuService;
//import com.kyhd.dshp.manage.service.UserService;
//import com.kyhd.dshp.manage.shiro.LoginType;
//import com.kyhd.dshp.manage.shiro.UserToken;
//import com.kyhd.dshp.manage.vo.Result;
//import com.kyhd.dshp.modules.model.Unit;
//import com.kyhd.dshp.modules.service.AdvertiserService;
//
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiImplicitParams;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//
//@RestController
//@Api(tags = "用户登录登出")
//@RequestMapping("/manage/api/user")
//public class LoginController {
//
//	
//	@Autowired
//    private UserService userService;
//	@Autowired
//	private MenuService menuService;
//	@Autowired
//	AdvertiserService advertiserService;
//    /**
//     * 	用户密码登录
//     * @param username
//     * @param password
//     * @return
//     */
//	@ApiOperation(value="用户登录接口")
//    @RequestMapping(value = "login", method = RequestMethod.POST)
//    public Result login(@RequestBody @ApiParam(name = "Login对象", value = "传入json格式", required = true) Login login){
//        UserToken token = new UserToken(LoginType.USER_PASSWORD, login.getUsername(), login.getPassword());
//        return shiroLogin(token,login.getUsername());
//    }
//
//
//    public Result shiroLogin(UserToken token,String username){
//        try {
//            //登录不在该处处理，交由shiro处理
//            Subject subject = SecurityUtils.getSubject();
//            subject.login(token);
//            //设置token过期时间一天（上线后删除）
//            subject.getSession().setTimeout(86400000);
//            if (subject.isAuthenticated()) {
//            	User user = userService.getUserByName(username);
//            	Map<String,Object> map = advertiserService.getAccountInformation(user.getId().toString());
//                JSON json = new JSONObject();
//                ((JSONObject) json).put("token", subject.getSession().getId());
//                ((JSONObject) json).put("userId", user.getId());
//                ((JSONObject) json).put("userName", user.getAccountName());
//                ((JSONObject) json).put("userType", user.getUserType());
//                ((JSONObject) json).put("profitPercentage", user.getProfitPercentage());
//                ((JSONObject) json).put("displayPercentage", user.getDisplayPercentage());
//                ((JSONObject) json).put("accountStatus", map.get("accountStatus"));
//                List<String> list = menuService.getAllMenuByUserId(user.getId());
//                ((JSONObject) json).put("permission", list);
//                return new Result(ResultStatusCode.OK, json);
//            }else{
//                return new Result(ResultStatusCode.SHIRO_ERROR);
//            }
//        }catch (IncorrectCredentialsException | UnknownAccountException e){
//            e.printStackTrace();
//            return new Result(ResultStatusCode.NOT_EXIST_USER_OR_ERROR_PWD);
//        }catch (LockedAccountException e){
//            return new Result(ResultStatusCode.USER_FROZEN);
//        }catch (Exception e){
//            return new Result(ResultStatusCode.SYSTEM_ERR,e);
//        }
//    }
//
//    /**
//     * 	退出登录
//     * @return
//     */
//    @ApiOperation(value="用户登出接口")
//    @RequestMapping(value = "logout", method = RequestMethod.POST)
//    @ApiImplicitParam(name = "x-access-token", value = "token令牌", required = true, dataType = "String",paramType="header")
//    public Result logout(HttpServletRequest request,HttpServletResponse response){
//        System.out.println(SecurityUtils.getSubject().isAuthenticated());
//        String username = ((User)SecurityUtils.getSubject().getPrincipal()).getUsername().toString();
//        System.out.println(username);
//    	if(SecurityUtils.getSubject().isAuthenticated()) {
//    		SecurityUtils.getSubject().logout();
//    		return new Result(ResultStatusCode.OK);
//    	}else {
//    		return new Result(ResultStatusCode.INVALID_TOKEN);
//    	}
//    }
//    
//    
//    /**
//     * 	根据userId获取对应的用户登录信息
//     */
//    @ApiOperation(value = "根据userId获取对应的用户信息")
//	@RequestMapping(value = "get_userinfo", method = RequestMethod.POST)
//	@ApiImplicitParam(name = "x-access-token", value = "token令牌", required = true, dataType = "String", paramType = "header")
//	public Result getUserInfo(@RequestParam String userId) {
//		if (SecurityUtils.getSubject().isAuthenticated()) {
//			try {
//				Integer id = ((User)SecurityUtils.getSubject().getPrincipal()).getId();
//            	User user = userService.getUserById(Integer.parseInt(userId));
//            	Map<String,Object> map = advertiserService.getAccountInformation(user.getId().toString());
//            	Map<String,Object> resultMap = new HashMap<String, Object>();
//                JSON json = new JSONObject();
//                ((JSONObject) json).put("token", SecurityUtils.getSubject().getSession().getId());
//                ((JSONObject) json).put("userId", user.getId());
//                ((JSONObject) json).put("userName", user.getAccountName());
//                ((JSONObject) json).put("userType", user.getUserType());
//                ((JSONObject) json).put("profitPercentage", user.getProfitPercentage());
//                ((JSONObject) json).put("displayPercentage", user.getDisplayPercentage());
//                ((JSONObject) json).put("accountStatus", map.get("accountStatus"));
//                List<String> list = menuService.getAllMenuIntoAccountByUserId(id);
//                ((JSONObject) json).put("permission", list);
//                User admin = userService.getUserById(id);
//            	Map<String,Object> adminMap = advertiserService.getAccountInformation(id.toString());
//                JSON adminJson = new JSONObject();
//                ((JSONObject) adminJson).put("token", SecurityUtils.getSubject().getSession().getId());
//                ((JSONObject) adminJson).put("userId", admin.getId());
//                ((JSONObject) adminJson).put("userName", admin.getAccountName());
//                ((JSONObject) adminJson).put("userType", admin.getUserType());
//                ((JSONObject) adminJson).put("profitPercentage", user.getProfitPercentage());
//                ((JSONObject) adminJson).put("displayPercentage", user.getDisplayPercentage());
//                ((JSONObject) adminJson).put("accountStatus", adminMap.get("accountStatus"));
//                List<String> adminList = menuService.getAllMenuByUserId(id);
//                ((JSONObject) adminJson).put("permission", adminList);
//                resultMap.put("advertiser", json);
//                resultMap.put("administrator", adminJson);
//                return new Result(ResultStatusCode.OK, resultMap);
//			} catch (Exception e) {
//				return new Result(ResultStatusCode.BAD_REQUEST, e);
//			}
//		} else {
//			return new Result(ResultStatusCode.INVALID_CLIENTID);
//		}
//	}
//    
//    
//}
